#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){	 
	// listen on the given port
	
	font.loadFont("arial.ttf", 8);
	bigfont.loadFont("arial.ttf", 35);
	
	
	//ofSetDataPathRoot("");
	cout<<ofToDataPath("../settings.xml", true)<<endl;
	if(!settings.loadFile("../settings.xml")){
		cout<<"--------"<<endl<<"ERROR: Could not load settings.xml (must be located in the same folder as this application)"<<endl<<"---------"<<endl<<endl;
	} else {
		cout<<"Settings loaded"<<endl;
	}
	settings.pushTag("settings", 0);
	int numClients = settings.getValue("numberclients",0,0);
	cout<<"Num clients: "<<numClients<<endl;
	string ip[numClients];
	//	ip[0] = 
	for(int i=0;i<numClients;i++){
		Client tmpClient;
		tmpClient.receiver.setup(14000+i);
		tmpClient.timer = 100;
		tmpClient.arduinoConnected = false;
		tmpClient.running = false;
		tmpClient.time = 0;
		tmpClient.cue = 0;
		tmpClient.numCues = 0;
		for(int u=0;u<3;u++){
			tmpClient.acc[u] = 0;
		}
		for(int u=0;u<4;u++){
			tmpClient.duskSum[u] = 0;
		}
		tmpClient.accSum = 0;
		tmpClient.v = "";
		tmpClient.length = 0;
		
		tmpClient.batPercent = -1;
		tmpClient.batTime = -1;
		tmpClient.senderConnected = false;
		tmpClient.lastMsgTime = 0;
		clients.push_back(tmpClient);
	}

	
	ofSetFrameRate(30);
	ofBackground( 20, 20, 20 );
	settings.popTag();
	cout<<"Setup finished"<<endl;
}

//--------------------------------------------------------------
void testApp::update(){
	
	// check for waiting messages
	for(int i=0;i<clients.size();i++){

		if(clients[i].senderConnected){
			ofxOscMessage m;
			m.setAddress( "/acc/master" );
			m.addIntArg(0);
			clients[i].sender.sendMessage(m);
		}

		while( clients[i].receiver.hasWaitingMessages() )
		{

			// get the next message
			ofxOscMessage m;
			
			clients[i].lastMsgTime = ofGetElapsedTimeMillis();
			
			clients[i].receiver.getNextMessage( &m );
			if(!clients[i].senderConnected){
				clients[i].ip = m.getRemoteIp();
				clients[i].sender.setup(m.getRemoteIp(), 13000+i);	
				clients[i].senderConnected = true;
			}
			
			//			cout<< m.getAddress().c_str()<<" "<<m.getArgAsInt32( 0)<<endl;
			clients[i].timer = 0;
			clients[i].netConnected = true;
			clients[i].arduinoConnected = m.getArgAsInt32(0);
			clients[i].running = m.getArgAsInt32(1); 
			clients[i].time = m.getArgAsFloat(2);
			clients[i].length = m.getArgAsFloat(3);
			clients[i].soundfile = m.getArgAsString(4);
			clients[i].cue = m.getArgAsInt32(5);
			clients[i].numCues = m.getArgAsInt32(6);
			clients[i].acc[0] = m.getArgAsFloat(7);
			clients[i].acc[1] = m.getArgAsFloat(8);
			clients[i].acc[2] = m.getArgAsFloat(9);
			clients[i].accSum = m.getArgAsFloat(10);
			clients[i].v = m.getArgAsString(11);			
			clients[i].batPercent = m.getArgAsInt32(12);
			clients[i].batTime = m.getArgAsInt32(13);	
			clients[i].accMaster = m.getArgAsInt32(14);	

			clients[i].duskSum[0] = m.getArgAsInt32(15);	
			clients[i].duskSum[1] = m.getArgAsInt32(16);	
			clients[i].duskSum[2] = m.getArgAsInt32(17);	
			clients[i].duskSum[3] = m.getArgAsInt32(18);	

			for(int u=0;u<3;u++){
				if(clients[i].acc[u] > 50){
					clients[i].acc[u] = 50;
				}
				if(clients[i].acc[u] < -50){
					clients[i].acc[u] = -50;
				}
				
			}
		}

		
		if(clients[i].timer > 100){
			clients[i].netConnected = false;
		}
		clients[i].timer ++;

	}

}


//--------------------------------------------------------------
void testApp::draw(){
	glPushMatrix();
	glTranslated(40, 40, 0);
	
	int r = ofGetWidth()-95;
	
	for(int i=0;i<clients.size();i++){
		glPushMatrix();
		
		
		
		ofFill();
		if(clients[i].netConnected)
			ofSetColor(0, 200, 0);
		else
			ofSetColor(200, 0, 0);
		ofRect(0, 0, 15, 100);
		glTranslated(15, 0, 0);		
		
		
		if(clients[i].running){
		ofSetColor(0, 100, 0);			
			ofRect(0, 0, r, 100);
		}
		
		
		ofSetColor(0, 150, 0);
		ofRect(0, 0, clients[i].time*r, 100);
		
		ofEnableAlphaBlending();
		
		if(!clients[i].arduinoConnected && ((int)(ofGetElapsedTimeMillis()/500))%2 == 0 && clients[i].lastMsgTime != 0){
			ofSetColor(200, 0, 0, 180);			
			ofRect(0, 0, r, 100);
		}
		
		ofSetColor(255, 255, 255);
		font.drawString(((clients[i].length*clients[i].time/60.0<10)?"0":"")+
						ofToString(clients[i].length*clients[i].time/60.0, 0)+
 						":"+((clients[i].length*clients[i].time-60*(int)(clients[i].length*clients[i].time/60)<10)?"0":"")+
						ofToString(clients[i].length*clients[i].time-60*(int)(clients[i].length*clients[i].time/60),0)+
						" / "+((clients[i].length/60.0<10)?"0":"")+
						ofToString(clients[i].length/60.0, 0)+
						":"+((clients[i].length-60*(int)(clients[i].length/60)<10)?"0":"")+
						ofToString(clients[i].length-60*(int)(clients[i].length/60),0), 2, 10);
		font.drawString("Cue: "+ofToString(clients[i].cue, 0)+" / "+ofToString(clients[i].numCues, 0), 2, 25);
		font.drawString("Ip: "+clients[i].ip, 2, 40);
		font.drawString("Latency: "+ofToString((ofGetElapsedTimeMillis()-clients[i].lastMsgTime)/1000.0,0)+" sec", 2, 55);
		
		int minutesLeft = floor((clients[i].length-clients[i].time*clients[i].length)/60.0);
		int secondsLeft = floor((clients[i].length-clients[i].time*clients[i].length)-minutesLeft*60.0);
		string pre = "";
		if(!clients[i].netConnected){
			minutesLeft = floor((clients[i].length-clients[i].time*clients[i].length-(ofGetElapsedTimeMillis()-clients[i].lastMsgTime)/1000.0)/60.0);
			secondsLeft = floor((clients[i].length-clients[i].time*clients[i].length-(ofGetElapsedTimeMillis()-clients[i].lastMsgTime)/1000.0)-minutesLeft*60.0);
		}
		string minutesLefts = ofToString(minutesLeft, 0);
		if(minutesLeft < 10)
			minutesLefts = "0"+minutesLefts;
		
		string secondsLefts= ofToString(secondsLeft, 0);
		if(secondsLeft < 10)
			secondsLefts = "0"+secondsLefts;
		if(clients[i].running && minutesLeft >= 0){
			bigfont.drawString(pre+"-"+minutesLefts+":"+secondsLefts, 200, 65);
		} else {
			bigfont.drawString("Pause", 200, 65);
		}


	
			font.drawString(clients[i].soundfile+"  "+ofToString(clients[i].accMaster, 0), 2, 100-5);
	
		
		ofSetColor(255, 255, 255);
		ofRect(r-4*20, 100, 19, -clients[i].duskSum[0]/2.0);
		ofRect(r-3*20, 100, 19, -clients[i].duskSum[1]/2.0);
		ofRect(r-2*20, 100, 19, -clients[i].duskSum[2]/2.0);		
		ofRect(r-1*20, 100, 19, -clients[i].duskSum[3]/2.0);
		
		font.drawString(clients[i].v, r-50, 100-5);
		
		
		if(clients[i].arduinoConnected){
			ofSetColor(0, 200, 0);
		} else {
			ofSetColor(200, 0, 0);
		}
		ofRect(r, 16, -80, 15);
		ofSetColor(255, 255, 255);
		font.drawString("DUSK", r-78, 25);
		
		string s;
		if(clients[i].running){
			ofSetColor(0, 200, 0);
			s = "PLAYING";
		} else {
			ofSetColor(200, 200, 0);
			s = "PAUSE";
		}
		ofRect(r, 0, -80, 15);
		ofSetColor(255, 255, 255);
		font.drawString(s, r-78, 10);
		
		
		
		if(clients[i].batPercent != -1){
			ofSetColor(0, 200, 0);
			if(clients[i].batPercent < 40){
				ofSetColor(200, 200, 0);
			}
			if(clients[i].batPercent < 20){
				ofSetColor(200, 0, 0);
			}
			s = ofToString(clients[i].batPercent,0)+"% / " + ofToString(clients[i].batTime/60,0)+"min";
			ofRect(r-80, 32, 80*clients[i].batPercent/100.0, 15);
		} else {
			ofSetColor(100, 100, 100);
			s = "-";
			ofRect(r, 32, -80, 15);
		}
		ofSetColor(255, 255, 255);
		ofNoFill();
		ofRect(r-79, 33, 78, 13);
		
		font.drawString(s, r-78, 41);
		
		
		
		
		ofNoFill();
		ofSetColor(255, 255, 255);
		ofRect(-15, 0, ofGetWidth()-80, 100);
		
		glPopMatrix();
		
		
		glTranslated(0, 105, 0);
	}
	
	glPopMatrix();
	
}


//--------------------------------------------------------------
void testApp::keyPressed  (int key){ 
	
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
	
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
	
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}

//--------------------------------------------------------------
void testApp::mouseReleased(){
}
